package com.zfast.yugioh.bo;

import cn.hutool.core.lang.WeightRandom;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.zfast.yugioh.constant.Define;
import com.zfast.yugioh.constant.DefineEnum;
import com.zfast.yugioh.constant.EffectsEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;


/**
 * 对局数据
 *
 * @author zgn
 * @email
 * @date 2022-06-10 15:16:47
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@ApiModel(value = "GameBo", description = "对局数据")
public class GameBo implements Serializable  {
    @ApiModelProperty("1号玩家对局信息/先手")
    GameDetailBo gameDetailBoPlayOne;

    @ApiModelProperty("2号玩家对局信息/后手")
    GameDetailBo gameDetailBoPlayTwo;

    @ApiModelProperty("回合数")
    @Builder.Default
    private Integer turn=1;

    @ApiModelProperty("阶段数")
    @Builder.Default
    private Integer phase=0;

    @ApiModelProperty("随机数")
    @Builder.Default
    Random r=new Random();

    @ApiModelProperty("拓展记录")
    @Builder.Default
    HashMap expandMap = new HashMap();

    /**
     * 展示阶段信息
     * @auther zgn
     * @date  2022/6/11
     **/
    public void showTurnInfo(){
        StringBuffer doingDecs=new StringBuffer("{");//动作描述
        String userName=turn%2==1?gameDetailBoPlayOne.getUserBo().getNickname():gameDetailBoPlayTwo.getUserBo().getNickname();
        doingDecs.append("第"+turn+"回合/"+userName+"/"+Define.turns[phase]);
        doingDecs.append("}");
        System.out.println(doingDecs);
    }

    /**
     * 抽卡
     * @auther zgn
     * @date  2022/6/11
     * @param player 玩家
     **/
    public String draw(GameDetailBo player){
        StringBuffer doingDecs=new StringBuffer("{");//动作描述

        String drawStr=Define.drawStr;
        List<CardBo> cards = player.getCards();
        List<CardBo> handCards = player.getHandCards();
        List<WeightRandom.WeightObj<CardBo>> weightList = new ArrayList<>();

        for (int i = 0; i < cards.size(); i++) {
            CardBo cardBo =  cards.get(i);
            WeightRandom.WeightObj<CardBo> a = new WeightRandom.WeightObj<>(cardBo, cardBo.getRate());
            weightList.add(a);
        }
        WeightRandom wr = RandomUtil.weightRandom(weightList);

        CardBo cardBo= (CardBo) wr.next();
        handCards.add(cardBo);
        doingDecs.append(drawStr+cardBo.getName());

        cards.remove(cardBo);

        doingDecs.append("}");
        return doingDecs.toString();
    }

    /**
     * 阶段描述
     * @auther zgn
     * @date  2022/6/11
     **/
    public void stepDecs(){
        StringBuffer doingDecs=new StringBuffer("{");//动作描述
        doingDecs.append("进入"+Define.turns[phase]);
        doingDecs.append("}");

        phase++;
        if(phase==6){
            phase=0;
            turn++;
        }

        System.out.println(doingDecs);
    }

    /**
     * 通常召唤
     * @auther zgn
     * @date  2022/6/11
     * @param player 玩家
     * @param cardBo 怪兽卡
     * @param status 召唤姿态
     **/
    public String normalSummon(GameDetailBo player, CardBo cardBo, int status){
        StringBuffer doingDecs=new StringBuffer("{");//动作描述

        List<CardBo> handCards = player.getHandCards();
        List<CardBo> liveCards = player.getLiveCards();

        handCards.remove(cardBo);//手卡移除
        cardBo.setFightStu(status);
        liveCards.add(cardBo);//入场

        doingDecs.append("以"+ DefineEnum.values()[status].getDescription()+"召唤"+cardBo.getName());
        doingDecs.append("[:"+showDialogue(cardBo)+"]");

        doingDecs.append("}");
        return doingDecs.toString();
    }

    /**
     * 使用魔法卡
     * @auther zgn
     * @date  2022/6/11
     * @param player 玩家
     * @param cardBo 魔法卡
     **/
    public String spell(GameDetailBo player,CardBo cardBo){
        StringBuffer doingDecs=new StringBuffer("{");//动作描述

        List<CardBo> handCards = player.getHandCards();
        List<CardBo> liveCards = player.getLiveCards();
        List<CardBo> graveyardCards = player.getGraveyardCards();

        handCards.remove(cardBo);
        liveCards.add(cardBo);

        doingDecs.append(showDialogue(cardBo));
        doingDecs.append(effect(player,cardBo));

        liveCards.remove(cardBo);
        graveyardCards.add(cardBo);


        doingDecs.append("}");
        return doingDecs.toString();
    }


    /**
     * 卡片出场台词
     * @auther zgn
     * @date  2022/6/10
     *
     * @param cardBo
     *
     **/
    public String showDialogue(CardBo cardBo){
        String dialogueSp = cardBo.getDialogueSp();
        dialogueSp= StrUtil.isNotBlank(dialogueSp)?dialogueSp:cardBo.getDialogue();
        if( StrUtil.isNotBlank(dialogueSp)){
            String[] split = dialogueSp.split(";");
            return split[r.nextInt(split.length)];
        }
        return "无卡片出场台词;";

    }

    /**
     * 发动效果
     * @auther zgn
     * @date  2022/6/11
     * @param player 玩家
     * @param cardBo 魔法卡
     **/
    public String effect(GameDetailBo player,CardBo cardBo){
        StringBuffer doingDecs=new StringBuffer("{");//动作描述

        //region 卡
        String effectStr = cardBo.getEffects();
        String[] effects = effectStr.split(";");
        for (int i = 0; i < effects.length; i++) {
            String[] effect = effects[i].split("_");

            switch(effect[0]){
                case "ck" ://抽卡
                    doingDecs.append("因为"+cardBo.getName()+"的效果:"+ EffectsEnum.getDescription(effect[0])+effect[1]+";");
                    for(int j=0;j<Integer.valueOf(effect[1]);j++){
                        doingDecs.append(draw(player)+";");
                    }
                    break;
                default : //可选
                    doingDecs.append("没有特效;");
            }
        }
        //endregion

        doingDecs.append("}");
        return doingDecs.toString();
    }

    /**
     * 复制粘贴
     * @auther zgn
     * @date  2022/6/11
     * @param player 玩家
     * @param cardBo 魔法卡
     **/
    public String demo(GameDetailBo player,CardBo cardBo){
        StringBuffer doingDecs=new StringBuffer("{");//动作描述

        doingDecs.append("}");
        return doingDecs.toString();
    }
}
